package com.example.a14874.listviewsqlite;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class StuAdapter extends BaseAdapter{
    private Context context;
    private ArrayList<Student> studentArrayList;
    private IoOnDataChangeListener listener;

    public StuAdapter(Context context,ArrayList<Student> studentArrayList,IoOnDataChangeListener listener){
        this.context=context;
        this.studentArrayList=studentArrayList;
        this.listener=listener;
    }
    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem( int position ) {
        return studentArrayList.get( position );
    }

    @Override
    public long getItemId( int position ) {
        return position;
    }

    @Override
    public View getView( int position, View view, ViewGroup parent ) {
        if (view==null){
            view= LayoutInflater.from( context ).inflate( R.layout.stu_item,
                    parent,false);
            StuViewHolder viewHolder=new StuViewHolder();
            viewHolder.tvStuno=view.findViewById( R.id.txt_id );
            viewHolder.tvname=view.findViewById( R.id.txt_name );
            viewHolder.tvAge=view.findViewById( R.id.txt_age );
            viewHolder.ivDel=view.findViewById( R.id.iv_del );

            view.setTag( viewHolder );
        }
        final Student student=studentArrayList.get( position );
        StuViewHolder viewHolder=(StuViewHolder) view.getTag();
        viewHolder.tvStuno.setText( student.getStuno() );
        viewHolder.tvname.setText( student.getName() );
        viewHolder.tvAge.setText( String.valueOf(student.getAge()  ) );

        viewHolder.ivDel.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                listener.del( student );
            }
        } );
        return view;
    }
}
